//
//  MemberSettingsController.m
//  myhuibao
//
//  Created by lijun on 13-3-7.
//  Copyright (c) 2013年 myhuibao. All rights reserved.
//

#import "MemberSettingsController.h"
#import <UIKit/UIKit.h>
#import "Config.h"
#import "ASIHTTPRequest.h"
#import "ASIFormDataRequest.h"
#import "SBJson.h"
@interface MemberSettingsController()

@end
@implementation MemberSettingsController
@synthesize mainview;
@synthesize lbtitle;
@synthesize oldPwd;
@synthesize confPwd;
@synthesize lbname;
@synthesize lbconfPwd;
@synthesize lbnewPwd;
static int mdfType;
@synthesize btsave;
@synthesize txnewPwd;
- (void)viewDidLoad
{
    [super viewDidLoad];
    mainview.hidden=YES;
    btsave.hidden=YES;
    // Do any additional setup after loading the view, typically from a nib.
}

- (IBAction)mdfPayPwd:(id)sender {
    mainview.hidden=NO;
    lbtitle.text=@"修改支付密码";
    mdfType=2;
    lbname.text=@"旧密码";
    oldPwd.placeholder=@"请输入原密码";
    lbconfPwd.hidden=NO;
    lbnewPwd.hidden=NO;
    txnewPwd.hidden=NO;
    confPwd.hidden=NO;
    btsave.hidden=NO;
    oldPwd.text=nil;
    txnewPwd.text=nil;
    confPwd.text=nil;
}

- (IBAction)goBack:(id)sender {
    if ([lbtitle.text isEqualToString:@"title"]) {
        [super.navigationController
         popViewControllerAnimated:YES];
    }else{
        mainview.hidden=YES;
        lbtitle.text=@"title";
        btsave.hidden=YES;
    }
    
}

- (IBAction)mdfloginpwd:(id)sender {
    mainview.hidden=NO;
    lbtitle.text=@"修改登陆密码";
    mdfType=1;
    lbname.text=@"旧密码";
    oldPwd.placeholder=@"请输入原密码";
    oldPwd.text=nil;
    txnewPwd.text=nil;
    confPwd.text=nil;
    lbconfPwd.hidden=NO;
    lbnewPwd.hidden=NO;
    txnewPwd.hidden=NO;
    confPwd.hidden=NO;
    btsave.hidden=NO;
}


- (void)dealloc {
    //[mainTable release];
    [super dealloc];
}

- (IBAction)doSave:(id)sender {
    if((mdfType==1||mdfType==2)&&![txnewPwd.text isEqualToString:confPwd.text]){
        UIAlertView*alert = [[UIAlertView alloc]initWithTitle:@"提示"
                                                      message:@"二次输入的密码不一致"
                                                     delegate:nil
                                            cancelButtonTitle:@"确定"
                                            otherButtonTitles:nil];
        [alert show];
        [alert release];
        txnewPwd.text=nil;
        confPwd.text=nil;
        return;
    }
    int memberId = [[Config Instance] getMemberId];
    NSString *password=[[Config Instance] getPassword];
    NSURL *url=nil;
    if(mdfType==1){
        url=[NSURL URLWithString:url_modloginPwd];
    }
    else if(mdfType==2) {
        url=[NSURL URLWithString:url_modpayPwd];
    }else if (mdfType==3){
        url=[NSURL URLWithString:url_modname];
    }
    else{
        url=[NSURL URLWithString:url_modnickname];
    }
    NSString *sid=[NSString stringWithFormat:@"%d",memberId];
    ASIFormDataRequest *request=
    [ASIFormDataRequest requestWithURL:url];
    if(mdfType==1){
        [request setPostValue: oldPwd.text forKey:@"oldPWD"];
        [request setPostValue: txnewPwd.text forKey:@"member.password"];
    }
    else if(mdfType==2){
        [request setPostValue: oldPwd.text forKey:@"oldPWD"];
        [request setPostValue: txnewPwd.text forKey:@"member.payPassword"];
        [request setPostValue:password forKey:@"member.password"];
    }
    else if(mdfType==3){
        [request setPostValue:password forKey:@"member.password"];
        [request setPostValue:oldPwd.text forKey:@"member.name"];
    }
    else{
        [request setPostValue:password forKey:@"member.password"];
        [request setPostValue:oldPwd.text forKey:@"member.nickName"];
    }
    [request setPostValue: sid
                   forKey:@"member.memberID"];
    [request setDelegate: self];
    [request startAsynchronous];
    
}

//下面是两个代理函数,当数据请求成功时会调用requestFinished，请求失败时（如网络问题或服务器内部错误）会调用requestFailed
- (void)requestFinished:(ASIHTTPRequest *)request
{
    NSString *jsonStr = [request responseString];
    //NSLog(@"%@",jsonStr);
    NSDictionary *dic = [jsonStr JSONValue];
    NSString *data = [dic valueForKey:@"status"];
    if([data isEqualToString:@"ERROR"]){
        data=[dic valueForKey:@"log"];
    }else{
        data=[dic valueForKey:@"data"];
        if(mdfType==1){
         [[Config Instance] setPassword:txnewPwd.text];
        }else if(mdfType==2){
            [[Config Instance] setPayPassword:txnewPwd.text];
        }else if (mdfType==3){
            [[Config Instance] setName:oldPwd.text];
        }else {
            [[Config Instance] setNickName:oldPwd.text];
        }
    }
    UIAlertView*alert =nil;
    if(mdfType==1||mdfType==2){
        alert=[[UIAlertView alloc]initWithTitle:@"提示"
                                        message:data
                                       delegate:nil
                              cancelButtonTitle:@"确定"
                              otherButtonTitles:nil];
        [alert show];
        [alert release];
    }else{
        if([[dic valueForKey:@"status"]isEqualToString:@"SUCCESS"]){
            data=@"修改成功";
        }else{
            data=@"修改失败";
        }
        alert=[[UIAlertView alloc]initWithTitle:@"提示"
                                        message:data
                                       delegate:nil
                              cancelButtonTitle:@"确定"
                              otherButtonTitles:nil];
        [alert show];
        [alert release];
    }
    /*
     UIAlertView*alert = [[UIAlertView alloc]initWithTitle:@"提示"
     message:[data valueForKey:@"data"]
     delegate:nil
     cancelButtonTitle:@"确定"
     otherButtonTitles:nil];
     [alert show];
     [alert release];
     */
    if([[dic valueForKey:@"status"] isEqualToString:@"ERROR"]){
        oldPwd.text=nil;
        txnewPwd.text=nil;
        confPwd.text=nil;
    }else{
        [self.navigationController
         popViewControllerAnimated:YES];
    }
    
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
    NSError *error = [request error];
    NSLog(@"%@",error);
}

- (IBAction)mdfName:(id)sender {
    mainview.hidden=NO;
    lbtitle.text=@"修改姓名";
    mdfType=3;
    lbname.text=@"新姓名";
    oldPwd.placeholder=@"输入您新的姓名";
    lbconfPwd.hidden=YES;
    lbnewPwd.hidden=YES;
    txnewPwd.hidden=YES;
    confPwd.hidden=YES;
    oldPwd.text=nil;
    btsave.hidden=NO;
}

- (IBAction)mdfNickName:(id)sender {
    mainview.hidden=NO;
    lbtitle.text=@"修改昵称";
    mdfType=4;
    lbname.text=@"新昵称";
    oldPwd.placeholder=@"输入您新的昵称";
    lbconfPwd.hidden=YES;
    lbnewPwd.hidden=YES;
    txnewPwd.hidden=YES;
    confPwd.hidden=YES;
    oldPwd.text=nil;
    btsave.hidden=NO;
}
@end
